In order to protect shared resources, Salesforce enforces a maximum number of SOQL queries, DML queries, @future
method, etc…
execution inside a single transaction.
This is part of Governor limits.
Every test method should check how close the tested code is to reach the governor limits. This is done by calling Test.StartTest()
before the tested code and Test.StopTest()
after it.
This rule raises an issue when a test method, i.e. a method annotated with @isTest
or testmethod
, does not contain a call
to Test.StartTest()
and Test.StopTest()
.
Noncompliant code example
@isTest()
public class MyTestClass {
@isTest()
static void myTestMethod() { // Noncompliant
// Preparing test (creating records, etc...)
// test code with no reference to Test.StartTest() or Test.StopTest()
}
static testmethod void myTestMethod2() { // Noncompliant
// Preparing test (creating records, etc...)
// test code with no reference to Test.StartTest() or Test.StopTest()
}
static void myHelper() {
// ...
}
}
Compliant solution
@isTest()
public class MyTestClass {
@isTest()
static void myTestMethod() {
// Preparing test (creating records, etc...)
Test.StartTest();
// test code with no reference to Test.StartTest() or Test.StopTest()
Test.StopTest();
}
static testmethod void myTestMethod2() {
// Preparing test (creating records, etc...)
Test.StartTest();
// test code with no reference to Test.StartTest() or Test.StopTest()
Test.StopTest();
}
static void myHelper() {
// ...
}
}
Exceptions
No issue will be raised if the test class, i.e. the class annotated with @isTest
, contains helper methods, i.e. methods not annotated
with @isTest
or testmethod
, which contain calls to Test.StartTest()
or Test.StopTest()
. This
indicates that the test code has been factorised and the rule would raise false positives.